<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<sc-form ref="dialogForm" :disabled="mode==='show' || disabled" :config="config" v-model="form"
				 :loading="loading"/>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			disabled: false,
			config: {},
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {},
			//所需数据选项
			groups: [],
			groupsProps: {
				value: "id",
				emitPath: false,
				checkStrictly: true
			}
		}
	},
	mounted() {
		this.config = {

			labelWidth: '100px',
			labelPosition: 'right',
			size: 'medium',
			formItems: [
				{
					label: "审批名称",
					name: "name",
					span: 12,
					component: "input",
				}, {
					label: "审批编号",
					name: "number",
					span: 12,
					component: "input",
				}, {
					label: "申请人",
					name: "applier",
					span: 12,
					component: "input",
				}, {
					label: "最后审批人",
					name: "lastApproval",
					span: 12,
					component: "input",
				}, {
					label: "创建时间",
					name: "createDate",
					span: 12,
					component: "date"
				}, {
					label: "最后操作时间",
					name: "lastApplyTime",
					span: 12,
					component: "date"
				}, {
					label: "金额",
					name: "amount",
					span: 12,
					component: "number",
					options: {
						precision: 2,
						step: 0.01,
					}
				}, {
					label: "申请状态",
					name: "state",
					span: 12,
					component: "select",
					options: {
						items: this.$tm('finance.approval.state')
					}
				}
			]
		}
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			if (mode === 'edit') {
				this.config.formItems.forEach((e) => {
					if (e.name !== 'state') {
						e.disabled = true
					}
				})
			}
			this.visible = true;
			return this
		},
		//表单提交方法
		async submit() {
			var valid = await this.$refs.dialogForm.validate().catch(() => {
			})
			if (!valid) {
				return false
			}
			this.isSaveing = true;
			if (this.mode === 'add') {
				await this.add()
			} else if (this.mode === 'edit') {
				await this.edit()
			}
			this.isSaveing = false;
		},
		async add() {
			// todo 调用save方法
			this.$TOOL.response(await this.$API.demo.post.post(this.form),
				(data) => {
					this.$emit('success', this.form, this.mode)
					this.visible = false;
					console.info(data)
				})
		},
		async edit() {
			// todo 调用update方法
			this.$TOOL.response(await this.$API.demo.post.post(this.form),
				(data) => {
					this.$emit('success', this.form, this.mode)
					this.visible = false;
					console.info(data)
				})
		},
		//表单注入数据
		setData(data) {
			// this.form.id = data.id
			// this.form.name = data.name
			// this.form.sex = data.sex
			// this.form.email = data.email
			// this.form.num = data.num
			// this.form.boolean = data.boolean

			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
